function personnelLocationCtrl($scope, $http, $timeout, $interval, paFetchService) {
  'ngInject';

  // 百度地图API功能
  let map = new BMap.Map("allmap");
  let currentPoint = new BMap.Point(119.436956, 32.165005);
  // map.centerAndZoom(currentPoint, 18);
  map.enableScrollWheelZoom();
  // map.disableScrollWheelZoom();

  let top_left_control = new BMap.ScaleControl({
    anchor: BMAP_ANCHOR_TOP_LEFT
  }); // 左上角，添加比例尺
  let top_left_navigation = new BMap.NavigationControl(); //左上角，添加默认缩放平移控件
  let top_right_navigation = new BMap.NavigationControl({
    anchor: BMAP_ANCHOR_TOP_RIGHT,
    type: BMAP_NAVIGATION_CONTROL_SMALL
  });
  map.addControl(top_right_navigation);

  this.staticsInfo = [];
  this.showStaticInfoDialog = true;
  this.queryData = {};

  let _this = $scope;
  let _timer = null;

  $scope.$on('setMapViewport', (e, worker) => {
    this.workerMarkers.forEach((marker) => {
        if (marker._sourceData.id == worker.id) {
          map.centerAndZoom(marker.point, 18);
          marker._triggleClick();
        }
      })
      // map.centerAndZoom(new BMap.)
  });

  //多边形
  this.addMutilShape = (rectDatas, type) => {
    if (rectDatas == null) {
      return;
    }
    let arry = [];
    rectDatas.forEach((point) => {
      arry.push(new BMap.Point(point.lng, point.lat));
    });
    let polygon = new BMap.Polygon(arry, {
      strokeColor: (type == 1 ? "green" : "red"),
      strokeWeight: 4,
      strokeOpacity: 1,
      fillOpacity: 1,
      fillColor: ""
    });
    map.addOverlay(polygon);
  }

  //圆形
  this.addCircleShape = (data) => {
    let circleData = data.cirleData,
      type = data.type,
      id = data.id;
    let point = new BMap.Point(circleData.lng, circleData.lat);
    let circle = new BMap.Circle(point, circleData.radius, {
      strokeColor: (type == 1 ? "green" : "red"),
      strokeWeight: 5,
      strokeOpacity: 0.5,
      fillOpacity: 1,
      fillColor: ""
    });
    circle.addEventListener('click', function() {
      $scope.$apply(function() {
        $scope.$broadcast('clickFenceArea', id);
      })
    })
    map.addOverlay(circle);
  }

  //标段切换
  this.tabBidItem = () => {

    //清除所有覆盖物
    map.clearOverlays();

    // this.getStaticsInfo();
    this.getPersonList();
    this.getElectricFence(true);

    this.startTimer();
  }

  //开启计时器，每次启动则清掉之前的计时器并重新开启
  this.startTimer = () => {
    if (this._timer) {
      this.stopTimer();
    };
    this._timer = $interval(function() {
      //清除所有覆盖物
      map.clearOverlays();
      // _this.vm.getStaticsInfo();
      _this.vm.getPersonList();
      _this.vm.getElectricFence(false);
    }, 60000);
  }

  this.stopTimer = () => {
    $interval.cancel(this._timer);
  }

  $scope.$on('$destroy', () => {
    this.stopTimer();
  });

  //获取电子围栏
  this.getElectricFence = (isFirst) => {
    var tenderId = this.queryData.bidId;
    $http
      .post(`${SERVER_CONTEXT}/api/electronFence/findAllFenceByCondition`, {
        tenderId: this.queryData.bidId
      })
      .success((resp) => {
        if (resp.success && resp.data && resp.data.length > 0) {
          let poiArray = [];
          resp.data.forEach((item) => {
            if (item.shape == 1) {
              poiArray.push(new BMap.Point(item.cirleData.lng, item.cirleData.lat));
              this.addCircleShape(item);
            } else {
              this.addMutilShape(item.rectDatas, item.type, item.id);
            }
          });
          // debugger;
          //切换标段第一次进来重新定位，定时刷新则不需要
          if (isFirst) {
            map.reset();
            map.setViewport(poiArray);
          }
        }
      })
  }


  // //获取统计信息
  // this.getStaticsInfo = () => {
  //  this.personSum = 0;
  //  let bidId = this.queryData.bidId;
  //  $http
  //    .get(`${SERVER_CONTEXT}/api/construction/getGISStatisticsInfo/` + bidId)
  //    .success((resp) => {
  //      if (resp.success) {
  //        this.staticsInfo = resp.data;
  //        this.staticsInfo.forEach((item) => {
  //          this.personSum += item ? parseInt(item.personNum) : 0;
  //        });
  //      }
  //    })
  // }

  //获取施工人员信息
  this.getPersonList = () => {
    let bidId = this.queryData.bidId;
    $http
      .get(`${SERVER_CONTEXT}/api/safesite/workerInfo/getGISPersonInfo/` + bidId)
      .success((resp) => {
        if (resp.success &&  resp.data) {
          /** added by sunqy for test demo */
          let lizhianData = {
            id: 17579,
            lat: 32.049123,
            lng: 119.661558,
            name: '杨成飞',
            quarters: '项目总工',
            status: 1,
            teamName: "无锡路桥集团股份有限公司",
          }
          resp.data.push(lizhianData);
          /* end */
          this.mapAddMarker(resp.data);
        }
      })
  }

  this.workerMarkers = [];
  // 向地图添加人员图标及信息
  this.mapAddMarker = (personList) => {
    if (personList && personList.length > 0) {
      personList.forEach((person) => {
        let point = new BMap.Point(person.lng, person.lat);
        let imageUrl = require(`../../../assets/images/person_location_yellow.png`);
        if (person.status == 3) {
          imageUrl = require(`../../../assets/images/person_location_red.gif`);
        }
        if (person.status == 2) {
          imageUrl = require(`../../../assets/images/person_location_green.gif`);
        }
        let marker = new BMap.Marker(point, {
          icon: new BMap.Icon(imageUrl, new BMap.Size(20, 20))
        });

        let opts = {
          title: "<h3>人员信息</h3>", // 信息窗口标题
        }
        let content = "<p>姓名：" + person.name + "</p>";
        content += "<p>岗位：" + person.quarters + "</p>";
        content += "<p>所属单位：" + person.teamName + "</p>";

        let infoWindow = new BMap.InfoWindow(content, opts); // 创建信息窗口对象 

        marker._sourceData = person;
        marker._triggleClick = function() {
          map.openInfoWindow(infoWindow, point);
        }
        this.workerMarkers.push(marker);

        marker.addEventListener("click", function() {
          map.openInfoWindow(infoWindow, point); //开启信息窗口
        });

        map.addOverlay(marker);

      });
    } else {
      // map.centerAndZoom(currentPoint,15);
    }
  }
}

export default personnelLocationCtrl;